<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class forget extends CI_Controller {
	var $data;
	public function __construct()
	{
		parent::__construct();		
		$this->load->library('session');
		$this->load->library('email');
		$this->load->model('model_forget');

		$this->data['title'] = 'Forget Password';
	}
	
	public function index()
	{

		$check = $this->session->userdata('CHECK_LOGIN');
		if($check==true){
			redirect(base_url().'dashboard/', 'refresh');
		}else{
			$this->view();
		}
		
	}

	public function view(){		
		if(isset($_POST["submit"])){
			$email=trim($_POST["email"]);
			if($email==""){
				$this->data['msg']="Email not null.";
			}else{
				// Check Tồn tại username					
				$result = $this->model_forget->selectExists(array('col'=>'mb_email','val'=>$email));						
				if(count($result)>0){
						$key=md5(md5("ABC".time()."DEF"));
						$this->model_forget->updateKey(array('key'=>$key,'email'=>$email));
						$this->data['msg']="Please check email!";
						$this->data['st']=true;
						// SEND EMAIL 
						$this->email->from('test@cc.com', 'Your Name');
						$this->email->to($email); 
						$this->email->subject('Change Password');
						$this->email->message('Click link http://develop.altamedia.vn:8990/projecta/changepass?key='.$key);	
						$this->email->send();

				}else{
					$this->data['msg']="Email does't exists.";		
				}
			}
		}
		
		$this->load->view('forget', $this->data);


	}
}

